<?php
/**
 * Description:微信入口
 * User: Administrator
 * Date: 2017-07-26
 * Time: 17:28
 */
namespace app\wxbackend\controller;
use app\wxbackend\controller\Wx\Oauth2;
use think\Controller;

class WxController Extends Controller
{
    public function load()
    {
        $wxLoad = new WxLoadController();
        if (isset($_GET['echostr'])) {
            $wxLoad->valid();
        } else {
            $wxLoad->handleRequest();
        }
        //
    }

    /**
     * @comment 获取微信code url
     * @author wuhao
     */
    public function getcode()
    {
        $path = I('path', '', 'strval');
        $weixin = new Oauth2();
        //$redirect_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $redirect_url = 'http://staticzft.louxiaomei.com/'.$path;
        $jumpurl = $weixin->oauth2_authorize($redirect_url, "snsapi_base", "123");
        $this->apiReturn(200, $jumpurl, '');
        //Header("Location: $jumpurl", true, 301);
        //exit();
    }

    /**
     * @comment oauth2 获取用户的openid
     * @author wuhao
     */
    public function oauth2()
    {
        $weixin = new Oauth2();
        $openid = "";
        if (!isset($_GET["code"])){
            $this->apiReturn(400, [], '未定义code');
        }else{
            $access_token = $weixin->oauth2_access_token($_GET["code"]);
            $openid = $access_token['openid'];
            $this->apiReturn(200, $openid, '');
            //return $openid;
        }
    }

    //返回给前端openid信息，为空的话重新去获取
    public function getOpenid()
    {
        if (!isset($_SESSION) || !$_SESSION) {
            $this->apiReturn(400, [], 'openid已失效');
        } else {
            $this->apiReturn(200, session('openid'), '');
        }
    }
}